package main

import (
	"context"
	"fmt"
	"sync"
	"time"
)

const (
	a = iota
	b
	c
)

type test struct {
	id int
}

var str string

var A, B int

var l sync.Mutex

func f() {
	A = 1
	B = 1
}

func g() {
	fmt.Println(B)
	fmt.Println(A)

}

var once sync.Once

var done bool

func setup() {
	str = "hello, world"
	done = true
}

func doprint() {
	if !done {
		once.Do(setup)
	}
	fmt.Println(str)
}

func main() {
	for {
		ctx, cancel := context.WithTimeout(context.Background(), time.Second)
		ok := make(chan int, 1)
		go func() {
			time.Sleep(500 * time.Millisecond)
			ok <- 1
		}()
		select {
		case <-ok:
			fmt.Println("yes")
			cancel()
		case <-ctx.Done():
			fmt.Println("no")
		}
		time.Sleep(100 * time.Millisecond)
	}
}

func channelTest1() {
	fmt.Println("1")
	go channelTest2()
	//time.Sleep(time.Second)
}

func channelTest2() {
	time.Sleep(time.Second * 2)
	fmt.Println("2")
}

func test1(mu sync.Mutex, mp map[string]string) {
	fmt.Println("test1: Wait Unlock")
	mu.Lock()
	fmt.Println("test1: Lock")
	mp["test1"] = "test1"
	mu.Unlock()
}
func test2(mu sync.Mutex, mp map[string]string) {
	fmt.Println("test2: Wait Unlock")
	mu.Lock()
	fmt.Println("test2: Lock")
	mp["test2"] = "test2"
	mu.Unlock()
}
